<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-row :gutter="20">
        <el-col :span="24" align="right">
          <el-form-item label="入库单号" prop="ksCode">
            <el-input
              v-model="queryParams.rkCode"
              placeholder="请输入库单号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['warehouse:cprk:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['warehouse:cprk:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['warehouse:cprk:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['warehouse:cprk:export']"-->
<!--        >导出-->
<!--        </el-button>-->
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cprkList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="入库单号" align="center" prop="rkCode" width="200"/>
      <el-table-column label="产品批次号" align="center" prop="cppcCode"/>
      <el-table-column label="产品编码" align="center" prop="scgdProductionWorkOrder.cpProduct.cpCode"/>
      <el-table-column label="产品名称" align="center" prop="scgdProductionWorkOrder.cpProduct.cpName"/>
      <el-table-column label="产品类型" align="center" prop="scgdProductionWorkOrder.cpProduct.cpLxcode" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.product_type" :value="scope.row.scgdProductionWorkOrder.cpProduct.cpLxcode"/>
        </template>
      </el-table-column>
      <el-table-column label="产品型号" align="center" prop="scgdProductionWorkOrder.cpProduct.cpType"/>
      <el-table-column label="产品分类" align="center"
                       prop="scgdProductionWorkOrder.cpProduct.cpflProductClassification.cpflName"/>
      <el-table-column label="产品品牌" align="center"
                       prop="scgdProductionWorkOrder.cpProduct.cpppProductBrand.ppName"/>
      <el-table-column label="入库仓库" align="center" prop="ckWarehouse.ckName"/>
      <el-table-column label="入库数量" align="center" prop="rkNum" width="80"/>
      <el-table-column label="入库时间" align="center" prop="rkTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.rkTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template slot-scope="scope">
          <el-button
          size="mini"
          type="text"
          icon="el-icon-edit"
          @click="handleExamine(scope.row)"
          v-hasPermi="['warehouse:llrk:examine']"
          v-if="scope.row.isUse===1"
        >审核
        </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['warehouse:cprk:edit']"
            v-if="scope.row.isUse===1"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['warehouse:cprk:remove']"
            v-if="scope.row.isUse===1"
          >删除
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetails(scope.row)"
            v-if="scope.row.isUse===0"
          >详情</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleCancel(scope.row)"
              v-if="scope.row.isUse===0"
            >撤销
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改产品入库对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" :inline="true">
        <el-form-item label="产品批次" prop="cppcCode">
          <el-input v-model="form.cppcCode" placeholder="请选择产品" readonly
                    @click.native="selectCpProduct()" suffix-icon="el-icon-more" style="width: 203px;">
          </el-input>
        </el-form-item>
        <el-form-item label="入库仓库" prop="ckWarehouse.ckName">
          <el-input v-model="form.ckWarehouse.ckName" placeholder="请选择入库仓库" readonly
                    @click.native="selectCkWarehouse()" suffix-icon="el-icon-more">
          </el-input>
        </el-form-item>
        <el-form-item label="产品型号" prop="cpType">
          <el-input v-model="form.scgdProductionWorkOrder.cpProduct.cpType" placeholder="请选择产品" readonly/>
        </el-form-item>
        <el-form-item label="入库时间" prop="rkTime">
          <el-date-picker clearable
                          v-model="form.rkTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择库时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="产品名称" prop="cpName">
          <el-input v-model="form.scgdProductionWorkOrder.cpProduct.cpName" placeholder="请选择产品" readonly/>
        </el-form-item>
        <el-form-item label="入库数量" prop="rkNum">
          <el-input v-model="form.rkNum" placeholder="请输入入库数量"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title1" :visible.sync="open1" width="40%">
          <el-container>
            <el-header>
              <el-input v-model="queryParams1.cpCode" suffix-icon="el-icon-search" @keyup.enter.native="peoUpsQuery"></el-input>
            </el-header>
            <el-main v-if="ckOpen">
              <!--展示仓库信息-->
              <el-table border :data="CkWarehouseList" @row-click="ckHandleRowClick" v-loading="loading1">
                <el-table-column prop="ckCode" label="仓库编号" width="140px"/>
                <el-table-column prop="ckName" label="仓库名称"/>
                <el-table-column prop="ckTypecode" label="仓库类型">
                  <template slot-scope="scope">
                    <dict-tag :options="dict.type.ck_type" :value="scope.row.ckTypecode"/>
                  </template>
                </el-table-column>
              </el-table>
              <pagination
                v-show="queryParams1.total>0"
                :total="queryParams1.total"
                :page.sync="queryParams1.pageNum"
                :limit.sync="queryParams1.pageSize"
                @pagination="selectCkWarehouse"
              />
            </el-main>
            <el-main v-if="cpOpen">
              <!--展示仓库信息-->
              <el-table border :data="CpProductList" @row-click="cpHandleRowClick" v-loading="loading1">
                <el-table-column prop="cppcCode" label="产品批次" width="140px"/>
                <el-table-column prop="scgdProductionWorkOrder.cpProduct.cpName" label="产品名称"/>
                <el-table-column prop="scgdProductionWorkOrder.cpProduct.cpType" label="产品型号"/>
              </el-table>
              <pagination
                v-show="queryParams1.total>0"
                :total="queryParams1.total"
                :page.sync="queryParams1.pageNum"
                :limit.sync="queryParams1.pageSize"
                @pagination="selectCpProduct"
              />
            </el-main>

          </el-container>
        </el-dialog>

    <!--  详情弹窗  -->
    <el-dialog title="入库详情" :visible.sync="open2" width="600px" append-to-body>
      <el-form ref="form" :model="form" label-width="120px" style="font-size: 10px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="产品批次：" prop="cppcCode">
              <span>{{ form.cppcCode }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="入库仓库：" prop="ckWarehouse.ckName">
              <span>{{ form.ckWarehouse.ckName }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="产品型号：" prop="cpType">
              <span>{{ form.scgdProductionWorkOrder.cpProduct.cpType }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="入库时间：" prop="rkTime">
              <span>{{ form.rkTime }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="产品名称：" prop="cpName">
              <span>{{ form.scgdProductionWorkOrder.cpProduct.cpName }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="入库数量：" prop="rkNum">
              <span>{{ form.rkNum }}</span>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="clickOpen2">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listCprk, getCprk, delCprk, addCprk, updateCprk, examine, selectCp, cancel} from "@/api/warehouse/cprk";
import {ckList} from "@/api/warehouse/llrk";
import {getCpfh} from "@/api/warehouse/cpfh";

export default {
  name: "Cprk",
  dicts: ['is_use', 'wl_type', 'wl_sortcode', 'ck_type', 'product_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      loading1:true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品入库表格数据
      cprkList: [],
      // 仓库表格数据
      CkWarehouseList: [],
      ckOpen:false,
      CpProductList:[],
      cpOpen:false,
      // 弹出层标题
      title: "",
      title1: "",
      // 是否显示弹出层
      open: false,
      open1: false,
      open2:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        rkCode: null
      },
      // 弹窗查询参数
      queryParams1: {
        pageNum: 1,
        pageSize: 10,
        total: 0,
        cpCode:null,
        ckName:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        cppcCode: [
          {required: true, message: "产品不能为空", trigger: "blur"}
        ],
        'ckWarehouse.ckName': [
          {required: true, message: "仓库不能为空", trigger: "blur"}
        ],
        rkNum: [
          {required: true, message: "入库数量不能为空", trigger: "blur"}
        ],
        rkTime: [
          {required: true, message: "入库时间不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询产品入库列表 */
    getList() {
      this.reset();
      this.loading = true;
      listCprk(this.queryParams).then(response => {
        console.log(response.rows)
        this.cprkList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        rkCode: null,
        cppcCode: null,
        ckCode: null,
        rkNum: null,
        rkMan: null,
        rkTime: null,
        isUse: null,
        isDelete: null,
        ckWarehouse: {
          ckCode: null,
          ckName: null
        },
        scgdProductionWorkOrder: {
          gdCppcCode: null,
          cpProduct: {
            cpName: null,
            cpType: null
          }
        }
      };
      // this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.rkCode)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加产品入库";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const rkCode = row.rkCode || this.ids
      getCprk(rkCode).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改产品入库";
      });
    },
    /** 详情 */
    handleDetails(row){
      this.reset();
      const fhCode = row.rkCode || this.ids1
      getCprk(fhCode).then(response => {
        this.form = response.data;
        this.open2 = true;
      });
    },
    clickOpen2(){
      this.open2=false
    },
    /** 撤销 */
    handleCancel(row){
      const rkCodes = row.rkCode;
      this.$modal.confirm('是否确认撤销来料入库编号为"' + rkCodes + '"的数据项？').then(function () {
        return cancel(rkCodes);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("撤销成功");
      }).catch(() => {
        this.$modal.msgWarning("库存不足,撤销失败");
      });
    },

    /** 审批 */
    handleExamine(row){
      const rkCodes = row.rkCode;
      this.$modal.confirm('是否确认审批来料入库编号为"' + rkCodes + '"的数据项？').then(function () {
        return examine(rkCodes);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("审批成功");
      }).catch(() => {
      });
    },
    /** 弹窗 */
    PopUpsShow(){
      this.open1=true
      this.loading1=true
      this.CkWarehouseList = null;
      this.CpProductList=null;
      this.cpOpen=false;
      this.ckOpen=false;
    },
    /**
     * 选择仓库
     * */
    selectCkWarehouse() {
      this.PopUpsShow()
      this.ckOpen=true;
      this.title1 = "选择仓库";
      ckList(this.queryParams1).then(response => {
        this.CkWarehouseList = response.rows;
        this.queryParams1.total = response.total;
        this.loading1=false
        this.queryParams1.ckName=null;
        this.queryParams1.cpCode=null;
      });
    },
    ckHandleRowClick(row){
      this.open1 = false
      this.form.ckCode=row.ckCode
      this.form.ckWarehouse.ckName=row.ckName
    },
    /**
     * 选择产品
     * */
    selectCpProduct() {
      this.PopUpsShow()
      this.cpOpen=true;
      this.title1 = "选择产品";
      selectCp(this.queryParams1).then(response => {
        this.CpProductList = response.rows;
        this.queryParams1.total = response.total;
        this.loading1=false
        this.queryParams1.ckName=null;
        this.queryParams1.cpCode=null;
      });
    },
    cpHandleRowClick(row){
      this.open1 = false
      this.form.cppcCode=row.cppcCode
      this.form.scgdProductionWorkOrder.cpProduct.cpName=row.scgdProductionWorkOrder.cpProduct.cpName
      this.form.scgdProductionWorkOrder.cpProduct.cpType=row.scgdProductionWorkOrder.cpProduct.cpType
    },
    peoUpsQuery(){
      this.queryParams1.ckName=this.queryParams1.cpCode
      if(this.cpOpen){
        this.selectCpProduct()
      }else if(this.ckOpen){
        this.selectCkWarehouse()
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.rkCode != null) {
            updateCprk(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCprk(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const rkCodes = row.rkCode || this.ids;
      this.$modal.confirm('是否确认删除产品入库编号为"' + rkCodes + '"的数据项？').then(function () {
        return delCprk(rkCodes);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('warehouse/cprk/export', {
        ...this.queryParams
      }, `cprk_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
